package cn.iocoder.yudao.module.pickbook.service.checkin;

import cn.iocoder.yudao.module.pickbook.dal.dataobject.checkin.PickbookCheckinDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.pickbook.dal.mysql.checkin.PickbookCheckinMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 打卡记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
@Slf4j
public class PickbookCheckinServiceImpl implements PickbookCheckinService {

    @Resource
    private PickbookCheckinMapper checkinMapper;

    @Override
    public PickbookCheckinDO createCheckin(Long userId, Long bookId, Long chapterId, Long classId,
                                         Integer readingTime, Integer pagesRead, String notes) {
        // 检查今日是否已打卡
        LocalDate today = LocalDate.now();
        if (hasCheckedInToday(userId, today)) {
            throw new RuntimeException("今日已打卡，不能重复打卡");
        }
        
        // 创建打卡记录
        PickbookCheckinDO checkin = PickbookCheckinDO.builder()
                .userId(userId)
                .bookId(bookId)
                .chapterId(chapterId)
                .classId(classId)
                .checkinDate(today)
                .readingTime(readingTime != null ? readingTime : 0)
                .pagesRead(pagesRead != null ? pagesRead : 0)
                .notes(notes)
                .status(1) // 正常状态
                .build();
        
        // 保存到数据库
        checkinMapper.insert(checkin);
        
        log.info("打卡记录创建成功，用户ID: {}, 绘本ID: {}, 打卡日期: {}", userId, bookId, today);
        
        return checkin;
    }

    @Override
    public boolean hasCheckedInToday(Long userId, LocalDate date) {
        return checkinMapper.selectByUserIdAndDate(userId, date) != null;
    }

    @Override
    public PageResult<PickbookCheckinDO> getUserCheckins(Long userId, Integer pageNo, Integer pageSize) {
        // 计算偏移量
        int offset = (pageNo - 1) * pageSize;
        
        // 查询总数
        long count = checkinMapper.selectCountByUserId(userId);
        
        // 查询列表
        List<PickbookCheckinDO> list = checkinMapper.selectListByUserId(userId, offset, pageSize);
        
        return new PageResult<>(list, count);
    }

    @Override
    public CheckinStats getCheckinStats(Long userId) {
        CheckinStats stats = new CheckinStats();
        
        // 获取总打卡天数
        stats.setTotalDays(checkinMapper.selectTotalDaysByUserId(userId));
        
        // 获取总阅读绘本数
        stats.setTotalBooks(checkinMapper.selectTotalBooksByUserId(userId));
        
        // 获取今日阅读绘本数
        stats.setTodayBooks(checkinMapper.selectTodayBooksByUserId(userId));
        
        // 获取本周阅读绘本数
        stats.setWeekBooks(checkinMapper.selectWeekBooksByUserId(userId));
        
        // 获取本月阅读绘本数
        stats.setMonthBooks(checkinMapper.selectMonthBooksByUserId(userId));
        
        return stats;
    }

    @Override
    public PageResult<PickbookCheckinDO> getClassCheckins(Long classId, Integer pageNo, Integer pageSize) {
        // 计算偏移量
        int offset = (pageNo - 1) * pageSize;
        
        // 查询总数
        long count = checkinMapper.selectCountByClassId(classId);
        
        // 查询列表
        List<PickbookCheckinDO> list = checkinMapper.selectListByClassId(classId, offset, pageSize);
        
        return new PageResult<>(list, count);
    }

    @Override
    public List<LocalDate> getCheckinCalendar(Long userId, LocalDate startDate, LocalDate endDate) {
        return checkinMapper.selectCheckinDatesByUserIdAndDateRange(userId, startDate, endDate);
    }
} 